package com.devilvan.betrice.sinofinance.controller;

import com.alibaba.fastjson.JSONObject;
import com.devilvan.betrice.common.BetriceResultVo;
import com.devilvan.betrice.exception.BetriceException;
import com.devilvan.betrice.request.BetriceHttpBuilder;
import com.devilvan.betrice.request.component.HttpClientComponent;
import com.devilvan.betrice.request.pojo.BetriceHttpClient;
import com.devilvan.betrice.sinofinance.po.SinaUsstockQuotesPo;
import com.devilvan.betrice.sinofinance.service.SinaUsstockQuotesService;
import com.devilvan.betrice.sinofinance.vo.SuqCrawlerRequestVo;
import com.devilvan.betrice.util.UEmpty;
import com.devilvan.betrice.util.UString;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Evad.Wu
 * @Description 新浪财经-美股实时行情信息表 控制器
 * @date 2023-03-19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "sinaUsstockQuotesController")
@Tag(name = "新浪财经-美股实时行情信息表 控制器")
public class SinaUsstockQuotesController {
    @Resource(name = "httpClientComponent")
    private HttpClientComponent httpClientComponent;
    @Resource(name = "threadPoolExecutor")
    private ThreadPoolExecutor threadPoolExecutor;

    @Resource(name = "sinaUsstockQuotesServiceImpl")
    private SinaUsstockQuotesService sinaUsstockQuotesService;

    /**
     * 总请求控制器
     *
     * @return 形式返回
     */
    @Operation(summary = "总请求控制器")
    @GetMapping(value = "doSinaUsstockQuotes")
    public BetriceResultVo<SinaUsstockQuotesPo> doSinaUsstockQuotes() {
        BetriceResultVo<SinaUsstockQuotesPo> betriceResultVo = new BetriceResultVo<>();
        return betriceResultVo;
    }

    /**
     * 采集报文控制器
     *
     * @param request 请求报文 {@link SuqCrawlerRequestVo}
     * @return 形式返回
     */
    @Operation(summary = "采集报文控制器")
    @PostMapping(value = "doSinaUsstockQuotesCrawler")
    public BetriceResultVo<SinaUsstockQuotesPo> doSinaUsstockQuotesCrawler(@RequestBody String request) {
        BetriceResultVo<SinaUsstockQuotesPo> betriceResultVo = new BetriceResultVo<>();
        SuqCrawlerRequestVo suqCrawlerRequestVo = JSONObject.parseObject(request, SuqCrawlerRequestVo.class);
        Optional.of(suqCrawlerRequestVo)
                .filter(f -> UEmpty.isNotEmpty(f.getRange()) || (f.getPage() != null && f.getPage() > 0))
                .orElseThrow(() -> new BetriceException("page和range不能都为空！"));
        Map<String, Object> params = new ConcurrentHashMap<>(7);
        params.put("num", suqCrawlerRequestVo.getNum());
        params.put("sort", "");
        params.put("asc", 0);
        params.put("market", suqCrawlerRequestVo.getMarket());
        params.put("id", suqCrawlerRequestVo.getId());
        Integer[] range = suqCrawlerRequestVo.getRange();
        String url = suqCrawlerRequestVo.getUrl();
        if (UEmpty.isNotEmpty(range)) {
            int start = range[0];
            int end = range[1];
            for (int i = start; i <= end; i++) {
                final int fi = i;
                params.put("page", fi);
                String paramUrl = UString.generateHttpParamUrlByUriBuilder(url, params);
                threadPoolExecutor.execute(() -> extracted(paramUrl));
            }
        } else {
            params.put("page", suqCrawlerRequestVo.getPage());
            String paramUrl = UString.generateHttpParamUrlByUriBuilder(url, params);
            extracted(paramUrl);
        }
        return betriceResultVo;
    }

    /**
     * 解析报文控制器
     *
     * @param request 报文
     * @return 形式返回
     */
    @Operation(summary = "解析报文控制器")
    @PostMapping(value = "doSinaUsstockQuotesRequest")
    public BetriceResultVo<SinaUsstockQuotesPo> doSinaUsstockQuotesRequest(@RequestBody String request) {
        return sinaUsstockQuotesService.doSinaUsstockQuotesRequest(request);
    }

    private void extracted(String paramUrl) {
        BetriceHttpClient betriceHttpClient = BetriceHttpBuilder.create()
                .setHttpRequest(RequestMethod.GET, paramUrl)
                .build();
        String body = httpClientComponent.doExecute(betriceHttpClient);
        this.doSinaUsstockQuotesRequest(body);
    }
}
